<?php

defined('SYSPATH') or die('No direct script access.');

// -- Environment setup --------------------------------------------------------
// Load the core Kohana class
require SYSPATH . 'classes/kohana/core' . EXT;

if (is_file(APPPATH . 'classes/kohana' . EXT)) {
	// Application extends the core
	require APPPATH . 'classes/kohana' . EXT;
} else {
	// Load empty core extension
	require SYSPATH . 'classes/kohana' . EXT;
}

/**
 * Set the default time zone.
 *
 * @see  http://kohanaframework.org/guide/using.configuration
 * @see  http://php.net/timezones
 */
date_default_timezone_set('America/Sao_Paulo');

/**
 * Set the default locale.
 *
 * @see  http://kohanaframework.org/guide/using.configuration
 * @see  http://php.net/setlocale
 */
setlocale(LC_ALL, 'en_US.utf-8');

/**
 * Enable the Kohana auto-loader.
 *
 * @see  http://kohanaframework.org/guide/using.autoloading
 * @see  http://php.net/spl_autoload_register
 */
spl_autoload_register(array('Kohana', 'auto_load'));

/**
 * Enable the Kohana auto-loader for unserialization.
 *
 * @see  http://php.net/spl_autoload_call
 * @see  http://php.net/manual/var.configuration.php#unserialize-callback-func
 */
ini_set('unserialize_callback_func', 'spl_autoload_call');

// -- Configuration and initialization -----------------------------------------

/**
 * Set the default language
 */
I18n::lang('pt-br');

/**
 * Set Kohana::$environment if a 'KOHANA_ENV' environment variable has been supplied.
 *
 * Note: If you supply an invalid environment name, a PHP warning will be thrown
 * saying "Couldn't find constant Kohana::<INVALID_ENV_NAME>"
 */
if (getenv('KOHANA_ENV') !== FALSE) {
	Kohana::$environment = constant('Kohana::' . strtoupper(getenv('KOHANA_ENV')));
}

/**
 * Initialize Kohana, setting the default options.
 *
 * The following options are available:
 *
 * - string   base_url    path, and optionally domain, of your application   NULL
 * - string   index_file  name of your index file, usually "index.php"       index.php
 * - string   charset     internal character set used for input and output   utf-8
 * - string   cache_dir   set the internal cache directory                   APPPATH/cache
 * - boolean  errors      enable or disable error handling                   TRUE
 * - boolean  profile     enable or disable internal profiling               TRUE
 * - boolean  caching     enable or disable internal caching                 FALSE
 */
Kohana::init(array(
	'base_url' => "/pandrade/",
	'errors' => Kohana::$environment !== Kohana::PRODUCTION,
	'profile' => Kohana::$environment !== Kohana::PRODUCTION,
	'caching' => FALSE,
//	'caching' => Kohana::$environment === Kohana::PRODUCTION,
));

/**
 * Attach the file write to logging. Multiple writers are supported.
 */
Kohana::$log->attach(new Log_File(APPPATH . 'logs'));

/**
 * Attach a file reader to config. Multiple readers are supported.
 */
Kohana::$config->attach(new Config_File);
	
/**
 * Enable modules. Modules are referenced by a relative or absolute path.
 */
Kohana::modules(array(
//	'i18nUrl' => MODPATH . 'i18nUrl', // Language on URL
	'grademagica' => MODPATH . 'grademagica', 
	'ajaxSubmit' => MODPATH . 'ajaxSubmit', // Submit forms using ajax
//	'auth'			=>	MODPATH.'auth',			// Basic authentication
	'cache' => MODPATH . 'cache', // Caching with multiple backends
	'database' => MODPATH . 'database', // Database access
	'image' => MODPATH . 'image', // Image manipulation
//	'lsi' => MODPATH . 'lsi', // Auto-categorization module
	'dec' => MODPATH . 'dec', // Dec Module
//	'orm'			=>	MODPATH.'orm',			// Object Relationship Mapping
//	'userguide' => MODPATH . 'userguide', // User guide and API documentation
	'pagination' => MODPATH . 'pagination', // Pagination module
	'smarty' => MODPATH . 'smarty', // Smarty module
//	'oauth'			=>	MODPATH.'oauth',		// OAuth module
//	'facebook'		=>	MODPATH.'facebook',		// Facebook controller for OAuth login
	'email' => MODPATH . 'email', // Email module
//	'unittest'		=>	MODPATH.'unittest',		// Unit testing
//	'codebench'		=>	MODPATH.'codebench',	// Benchmarking tool
));

if(!Kohana::$errors){
    set_exception_handler(array('Exception_Handler_Dec', 'handle'));
}
Cookie::$salt = "aflkjwiuxcbkd";

/**
 * Load language conf
 */
if (Arr::get(Kohana::modules(), "i18nUrl")) {
	$langs = Kohana::$config->load('lang.lang_uri_abbr');
	$default_lang = Kohana::$config->load('lang.language_abbr');
	$acceptLanguage = explode(",", strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']));
	foreach ($acceptLanguage as $language) {
		if (($pos = strpos($language, ";")) !== FALSE) {
			$language = substr($language, 0, $pos);
		}
		if (isset($langs[$language])) {
			$default_lang = $language;
			break;
		}
	}
	/**
	 * Set the default language
	 */
	$default_lang = Cookie::get('lang', $default_lang);
	I18n::lang($default_lang);
	$lang_ignore = Kohana::$config->load('lang.lang_ignore');
	$langs_abr = implode('|', array_keys($langs));
	if (!empty($langs_abr))
		$langs_abr .= '|' . $lang_ignore;

	/**
	 * Set the routes. Each route must have a minimum of a name, a URI and a set of
	 * defaults for the URI.
	 */
	Route::set('sections', '(<lang>)(/)<directory>(/<controller>(/<action>(/<id>)))', array(
				'directory' => '(adm|base|action)',
				'lang' => "({$langs_abr})",
				'id' => '.+'
			))
			->defaults(array(
				'lang' => $default_lang,
				'controller' => 'home',
				'action' => 'index',
			));

	Route::set('default', '(<lang>(/<controller>(/<action>(/<id>))))', array(
				'lang' => "({$langs_abr})",
				'id' => '.+'
			))
			->defaults(array(
				'lang' => $default_lang,
				'controller' => 'home',
				'action' => 'index',
			));
//    die("ali");
} else {

	/**
	 * Set the routes. Each route must have a minimum of a name, a URI and a set of
	 * defaults for the URI.
	 */
//        die("aqui");
	Route::set('sections', '(/)<directory>(/<controller>(/<action>(/<id>)))', array(
				'directory' => '(external|adm/action/external|adm/action|action|adm|base)',
				'id' => '.+'
			))
			->defaults(array(
				'controller' => 'home',
				'action' => 'index',
			));

	Route::set('default', '(<controller>(/<action>(/<id>)))', array(
				'id' => '.+'
			))
			->defaults(array(
				'controller' => 'home',
				'action' => 'index',
			));
}